interface IPageData {
  rankList: Array<{
    id: number;
    title: string;
    imageUrl: string;
    readCount: number;
    collectCount?: number;
    createTime?: string;
  }>;
  activeTab: number;
}

Page<IPageData>({
  data: {
    activeTab: 0,
    rankList: [
      {
        id: 1,
        title: '小红帽历险记',
        imageUrl: 'https://images.unsplash.com/photo-1502307100811-6bdc0981a85b?w=400&h=300&fit=crop&q=80',
        readCount: 12345
      },
      {
        id: 2,
        title: '睡美人',
        imageUrl: 'https://images.unsplash.com/photo-1516280440614-37939bbacd81?w=400&h=300&fit=crop&q=80',
        readCount: 10234
      },
      {
        id: 3,
        title: '白雪公主',
        imageUrl: 'https://images.unsplash.com/photo-1578662996442-48f60103fc96?w=400&h=300&fit=crop&q=80',
        readCount: 9876
      },
      {
        id: 4,
        title: '灰姑娘',
        imageUrl: 'https://images.unsplash.com/photo-1599458252573-56ae36120de1?w=400&h=300&fit=crop&q=80',
        readCount: 8765
      }
    ]
  },

  onLoad() {
    // 页面加载时的初始化逻辑
    this.fetchRankList();
  },

  async fetchRankList() {
    try {
      let rankData;
      if (this.data.activeTab === 0) {
        // 热门阅读
        const response = await new Promise((resolve, reject) => wx.request({
          url: 'https://fairy.ymdx.cn/v1/fairy/findTop6Views',
          method: 'POST',
          data: {},
          success: function(res) {
            resolve(res);
          },
          fail: function(err) {
              reject(err);
          }
        }));
        
        if (!response.data || response.data.code === 200) {
          rankData = response.data.data.map(item => ({
            id: item.id,
            title: item.name,
            imageUrl: item.thumbnail,
            readCount: item.views,
            showText: '阅读量'
          }));
        } else {
          throw new Error('获取热门阅读数据失败');
        }
      } else if (this.data.activeTab === 1) {
        // 热门收藏
        const response = await new Promise((resolve, reject) => wx.request({
          url: 'https://fairy.ymdx.cn/v1/fairy/findTop6Collects',
          method: 'POST',
          data: {},
          success: function(res) {
            resolve(res);
          },
          fail: function(err) {
              reject(err);
          }
        }));
        
        if (!response.data || response.data.code === 200) {
          rankData = response.data.data.map(item => ({
            id: item.id,
            title: item.name,
            imageUrl: item.thumbnail,
            readCount: item.collects,
            showText: '收藏量'
          }));
        } else {
          throw new Error('获取热门阅读数据失败');
        }
      } else {
        // TODO: 实现最新发布接口
        rankData = [];
      }

      this.setData({
        rankList: rankData
      });
    } catch (error) {
      console.error('获取排行榜数据失败:', error);
      wx.showToast({
        title: '数据加载失败',
        icon: 'none'
      });
    }
  },



  switchTab(event: WechatMiniprogram.TouchEvent) {
    const tab = Number(event.currentTarget.dataset.index);
    if (tab >= 0 && tab <= 2) {
      this.setData({ activeTab: tab }, () => {
        this.fetchRankList();
      });
    }
  },

  viewStory(event: WechatMiniprogram.TouchEvent) {
    const storyId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/story/story?id=${storyId}`
    });
  },

  onPullDownRefresh() {
    // 下拉刷新
    this.fetchRankList();
    wx.stopPullDownRefresh();
  },

  onShareAppMessage() {
    return {
      title: '童话大全 - 故事排行榜',
      path: '/pages/rank/rank'
    };
  }
});